/**********************************************************************
 * SailmiPIM - modular cross-platform feature Personal Information Manager Tools.
 * Copyright(C) 2006-2014  Georg Rudoy
 *
 * Distributed under the Boost Software License, Version 1.0.
 *(See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
 **********************************************************************/

#include "restoresessiondialog.h"
#include <util/sll/qtutil.h>
#include <sailmipim/iinfo.h>

namespace SailmiPIM::Plugin::TabSessManager
{
	RestoreSessionDialog::RestoreSessionDialog(QWidget *parent)
	: QDialog(parent)
	{
		Ui_.setupUi(this);
        qDebug() << "222222222222222222222222222222";
		connect(Ui_.SelectAll_,
				&QPushButton::released,
				[this] { CheckAll(Qt::Checked); });
		connect(Ui_.SelectNone_,
				&QPushButton::released,
				[this] { CheckAll(Qt::Unchecked); });
	}

	void RestoreSessionDialog::SetTabs(const QHash<QObject*, QList<RecInfo>>& pages)
	{
		for(const auto [obj, infos] : Util::Stlize(pages))
		{
			const auto ii = qobject_cast<IInfo*>(obj);

			auto parent = new QTreeWidgetItem({ ii->GetName() });
			parent->setIcon(0, ii->GetIcon());
			parent->setData(0, Qt::UserRole, QVariant::fromValue(obj));
			Ui_.Tabs_->addTopLevelItem(parent);

			for(const auto& info : infos)
			{
				const auto& name = info.Name_.isEmpty() ?
						'<' + tr("no name") + '>' :
						info.Name_;
				auto item = new QTreeWidgetItem({ name });
				item->setIcon(0, info.Icon_);
				item->setCheckState(0, Qt::Checked);
				item->setData(0, Qt::UserRole, QVariant::fromValue(info));
				parent->addChild(item);
			}

			Ui_.Tabs_->expandItem(parent);
		}
	}

	QHash<QObject*, QList<RecInfo>> RestoreSessionDialog::GetTabs() const
	{
		QHash<QObject*, QList<RecInfo>> result;
        qDebug() << "3222222222222222222222222222222";

		for(int i = 0, size = Ui_.Tabs_->topLevelItemCount(); i < size; ++i)
		{
			const auto parent = Ui_.Tabs_->topLevelItem(i);

			QList<RecInfo> infos;
			for(int j = 0, jsize = parent->childCount(); j < jsize; ++j)
			{
                qDebug() << "422222222222222222222222222222";
                auto child = parent->child(j);

				if(child->checkState(0) != Qt::Checked)
					continue;

				infos << child->data(0, Qt::UserRole).value<RecInfo>();
            }
            qDebug() << "522222222222222222222222222222";

			result [parent->data(0, Qt::UserRole).value<QObject*>()] = infos;
		}
        qDebug() << "622222222222222222222222222222";

		return result;
	}

	void RestoreSessionDialog::CheckAll(Qt::CheckState state)
	{
		for(int i = 0, size = Ui_.Tabs_->topLevelItemCount(); i < size; ++i)
		{
			auto parent = Ui_.Tabs_->topLevelItem(i);
			for(int j = 0, jsize = parent->childCount(); j < jsize; ++j)
				parent->child(j)->setCheckState(0, state);
		}
	}
}
